﻿using System;

namespace WebDiagnostics
{
    public class DiagnoserDescriptorAttribute : Attribute
    {
        public string DisplayName { get; set; }
        
        public object Priority { get; set; }

        public object Width { get; set; }


        public DiagnoserDescriptorAttribute(string displayName)
        {
            DisplayName = displayName;
        }
    }

    public enum RenderWidth
    {
        Half = 0,
        Full = 1
    }

    internal class AttributedDiagnoserDescriptor : DiagnoserDescriptor
    {
        private readonly Type _diagnoserType;
        private readonly string _displayName;
        private readonly int? _priority;

        public override Type DiagnoserType
        {
            get { return _diagnoserType; }
        }

        public override string DisplayName
        {
            get { return _displayName; }
        }

        public override int Priority
        {
            get { return _priority.GetValueOrDefault(base.Priority); }
        }

        public AttributedDiagnoserDescriptor(Type diagnoserType, DiagnoserDescriptorAttribute attribute)
        {
            _diagnoserType = diagnoserType;
            _displayName = attribute.DisplayName;
            _priority = (attribute.Priority as int?);
            Width = (attribute.Width as RenderWidth?).GetValueOrDefault(RenderWidth.Half);
        }
    }


    public abstract class DiagnoserDescriptor
    {
        public abstract Type DiagnoserType { get; }

        public abstract string DisplayName { get; }

        public virtual int Priority
        {
            get { return 50; }
        }

        public virtual RenderWidth Width { get; protected set; }

        public virtual object[] Arguments
        {
            get { return new object[0]; }
        }

        private const string LoaderFunctionTemplate =
            "function load_{0}() {{ $.ajax({{ url: '{1}', success: function(data) {{ $('#{0}').html(data); }} }} ) }}";
        private const string ScriptTemplate =
            "$(function() {{ setInterval('load_{0}()', {1}); load_{0}(); }});";
        public virtual string LoaderScript
        {
            get
            {
                string loaderFunction = string.Format(LoaderFunctionTemplate, Key, DataUrl);
                string script = string.Format(ScriptTemplate, Key, RefreshTimeout.TotalMilliseconds);
                
                return string.Join(" ", new [] { loaderFunction, script });
            }
        }

        public virtual string ClientDataTemplate
        {
            get { return string.Empty; }
        }

        public virtual string Layout
        {
            get
            {
                return string.Format(@"
                    <div id='{0}'>
                        <h2 class='head'>{1}</h2>
                        <div class='body'></div>
                        <div class='footer'></div>
                    </div>",
                    Key, DisplayName);
            }
        }

        public virtual string Key
        {
            get { return DiagnoserType.Name; }
        }

        public virtual string DataUrl
        {
            get { return "?diagnoser="+Key; }
        }

        public virtual TimeSpan RefreshTimeout
        {
            get { return TimeSpan.FromSeconds(30); }
        }

    }
}
